//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "ia\fn_kamikaze.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_kamikaze","_units"];
DEFAULT_PARAM(_kamikaze,0,objNull);

if (IS_GROUP(_kamikaze)) then {
	_units = units _kamikaze;
	_kamikaze = _units select (floor (random (count _units)));
};
if (IS_ARRAY(_kamikaze)) then {
	if (count _kamikaze > 0) then {
		_kamikaze = _kamikaze select (floor (random (count _kamikaze)));
	} else {
		_kamikaze = objNull;
	};
};
if (AND_ALSO((!isnull _kamikaze),IS_OBJECT(_kamikaze))) then {
	if (!isServer && !(local _kamikaze)) exitWith { ERROR_1("Execution should be on server or local to the unit",_this); };
	_this set [0,_kamikaze];
	_this spawn {
		private ["_side","_param","_kamikaze","_units","_distance","_ammo","_trust","_trigger","_destroy","_pos","_explo"];
		DEFAULT_PARAM(_kamikaze,0,objNull);
		DEFAULT_PARAM(_side,1,GVAR(side_own));
		DEFAULT_PARAM(_distance,2,30);
		DEFAULT_PARAM(_ammo,3,"R_60mm_HE");
		DEFAULT_PARAM(_trust,4,1);
		_kamikaze allowFleeing 0;
		_kamikaze setskill ["courage",1];
		_kamikaze setskill ["spotDistance",0];
		_kamikaze setskill ["spotTime",0];
		_kamikaze disableAI "AUTOTARGET";
		_kamikaze disableAI "TARGET";
		_kamikaze setVariable [QGVAR(Kamikaze),[_side,_distance,_ammo,_trust]];
		_trigger = false;
		_destroy = false;
		_target = objNull;
		DEBUG_1("Created",ARR_6(_kamikaze, typeof _kamikaze, getPos _kamikaze, _distance, _ammo, _trust));
		while {!_trigger && !(isNull _kamikaze)} do {
			_pos = getPosASL _kamikaze;
			_pos set [2,0];
			_units = _pos nearEntities [["Man","Car","Tank","Motorcycle"],_distance];
			if (count _units > 0) then {
				{	if ((side _x == _side) && alive _x) exitWith {_trigger = true; _target = _x;};
					sleep 0.01;
				} forEach _units;
			};
			if ((damage (vehicle _kamikaze) > 0.7) || !(alive _kamikaze)) then {_trigger = true;_destroy = true;};
			if (_trigger) then {
				TRACE_1("Target Found",_target);
				if (!isnull _target && (alive _kamikaze)) then {
					_i_runs = 30;
					// We ungroup the Kamikaze in case he is a Group Leader to avoid taking the whole group out with him.
					[_kamikaze] joinSilent grpNull;
					TRACE_1("Kamikaze Degrouped",ARR_2(_kamikaze,group _kamikaze));
					_kamikaze setSpeedMode "FULL";
					_kamikaze doMove (getPos _target);
					sleep 0.5123;
					_kamikaze doMove (getPos _target);
					_last_target_pos = getPos _target;
					_last_kam_pos = getPos _kamikaze;
					while { (_target distance _kamikaze) >= 10 && _i_runs > 0 && (alive _kamikaze)} do {
						if ((_last_target_pos distance _target) > 3 || (_last_kam_pos distance _kamikaze) < 1) then {
							_last_target_pos = getPos _target;
							_kamikaze doMove (getPos _target);
						};
						_last_kam_pos = getPos _kamikaze;
						sleep 0.5123;
					};
				};
				(random 1);
				if (!(alive _kamikaze)) then { _destroy = true; };
				if (((random 1 <= _trust) && (_trust > 0)) || _destroy) then {
					_distance = 0;
					if (alive _kamikaze) then {
						_distance = (speed _kamikaze) / 3;
						_pos = [_kamikaze,_distance,direction _kamikaze] call BIS_fnc_relPos;
					} else {
						_pos = getPos _kamikaze;
					};
					if (vehicle _kamikaze != _kamikaze) then {(vehicle _kamikaze) setDamage 1; };
					if (IS_ARRAY(_ammo)) then {
						_ammo = _ammo call FFUNC(arrayRandomVal);
					};
					TRACE_1("Creating Bomb",ARR_2(_ammo,_pos));
					_explo = createVehicle [_ammo,_pos,[],0,"CAN_COLLIDE"];
					_explo attachTo [_kamikaze,[0,0,-1.2]];
					sleep 0.1;
					_explo setDamage 1;
					waituntil { !alive _kamikaze || (damage _explo) > 0 };
					_kamikaze setDamage 1;
				} else {
					_pos = position _kamikaze;
					TRACE_1("Created Smoke",ARR_2("SmokeShell",_pos));
					_fumi = createVehicle ["SmokeShell",_pos,[],0,"CAN_COLLIDE"];
					_fumi setDamage 1;
					_fumi attachTo [_kamikaze,[0,0,0.5]];
					sleep 3;
					deleteVehicle _fumi;
				};
			};
			sleep 1;
		};
	};
} else {
	ERROR_1("Invalid Kamikaze",_kamikaze);
	_kamikaze = objNull;
};
TRACE_1("End",_kamikaze);
TMFPROFILERSTOP;
_kamikaze